package prehilife;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class GUI extends JFrame {

    private boolean auto;
    private long waitTime;
    public AbstractWorld theWorld;
    private JLabel labelHolder[][];
    private JPanel panel;
    private JButton buttNext;
    private int sizeX, sizeY;
    private boolean graphicMode;
    private boolean block=true;
    public GUI() {
    }

    private void initComponents() {
        setTitle("PrehiLife");
        panel = new javax.swing.JPanel();
        buttNext=new JButton();
        buttNext.setText("Next Step");
        if(auto){
            buttNext.setEnabled(false);
        }
        buttNext.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                makeMeFree();
            }
        });
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        BorderLayout layout=new BorderLayout();
        getContentPane().setLayout(layout);
        setPreferredSize(new Dimension(400, 300));
        labelHolder = new JLabel[sizeX][sizeY];
        panel.setLayout(new GridLayout(sizeX, sizeY));
        for (int m = 0; m < sizeX; m++) {
            for (int n = 0; n < sizeY; n++) {
                labelHolder[m][n] = new JLabel();
                panel.add(labelHolder[m][n]);
            }
        }
        add(panel, BorderLayout.CENTER);
        add(buttNext,BorderLayout.SOUTH);
//        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
//        getContentPane().setLayout(layout);
//        layout.setHorizontalGroup(
//                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE));
//        layout.setVerticalGroup(
//                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(panel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE));

        pack();
    }
    synchronized void makeMeFree(){
        notify();
    }
    synchronized void isBlocked(){
        try {
            wait();
        } catch (InterruptedException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void drawNextStep() {
        if (graphicMode) {
            clearPanel();
            for (int i = 0; i < sizeX; i++) {
                for (int j = 0; j < sizeY; j++) {
                    Iterator<Creature> arrayCreature = theWorld.myGameBoard[i][j].iterator();
                    while (arrayCreature.hasNext()) {
                        Creature c = arrayCreature.next();
                        JLabel image = new JLabel();
                        ImageIcon icon;
                        if (c instanceof Grass) {
                            icon = new ImageIcon(getClass().getResource("/images/grass.png"));
                            image.setIcon(icon);
                            image.setText(null);
                        }else if (c instanceof Mammouth) {
                            icon = new ImageIcon(getClass().getResource("/images/mammouth.png"));
                            image.setIcon(icon);
                            image.setText(null);

                        } else if (c instanceof Hut) {
                            icon = new ImageIcon(getClass().getResource("/images/hutte.png"));
                            image.setIcon(icon);
                            image.setText(null);
                        } else if (c instanceof Member) {
                            Member m = (Member) c;
                            if (m.isChief()) {
                                icon = new ImageIcon(getClass().getResource("/images/chef.png"));
                                image.setIcon(icon);
                                image.setText(null);
                            } else {
                                icon = new ImageIcon(getClass().getResource("/images/membre.png"));
                                image.setIcon(icon);
                                image.setText(null);
                            }
                        } else if (c instanceof Cave) {
                            icon = new ImageIcon(getClass().getResource("/images/grotte.png"));
                            image.setIcon(icon);
                            image.setText(null);
                        } else {
                            icon = new ImageIcon();
                        }

                        labelHolder[i][j].setIcon(icon);
                    }
                }
            }
        } else {
            drawNextStepText();
        }
    }

    private void drawNextStepText() {
        for (int i = 0; i < sizeY; i++) {
            for (int j = 0; j < sizeX; j++) {
                Iterator<Creature> arrayCreature = theWorld.myGameBoard[j][i].iterator();
                                
                while (arrayCreature.hasNext()) {
                    Creature c = arrayCreature.next();
                    if (c instanceof Grass) {
                    } else if (c instanceof Mammouth) {
                        System.out.print("M");
                    } else if (c instanceof Hut) {
                        System.out.print("H");
                    } else if (c instanceof Member) {
                        Member m = (Member) c;
                        if (m.isChief()) {
                            System.out.print("c"+m.getHp());
                        } else {
                            System.out.print("m"+m.getHp());
                        }
                    } else if (c instanceof Cave) {
                        System.out.print("C");
                    }
                }
                System.out.print("+-+");
            }
            System.out.println("");
        }
        System.out.println("");
        System.out.println("");
        System.out.println("");
    }

    public void drawEnd(String endMsg) {
        if(graphicMode){
            JOptionPane.showMessageDialog(this, endMsg);  
        }else{
            System.out.println(endMsg);
        }
    }

    private void clearPanel() {
        for (int i = 0; i < labelHolder.length; i++) {
            for (int j = 0; j < labelHolder[i].length; j++) {
                labelHolder[i][j].setIcon(null);
            }
        }
    }

    void configure(PrehiWorld theWorld, int waitTime, boolean auto, boolean graphicMode) {
        this.theWorld = theWorld;
        this.waitTime = waitTime;
        this.auto = auto;

        sizeX = theWorld.getSizeX();
        sizeY = theWorld.getSizeY();
        if (graphicMode) {
            initComponents();
            this.setVisible(graphicMode);
        }
        this.graphicMode = graphicMode;
    }
}